//
//  LCLoginViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/11.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCLoginViewController.h"
#import "ModifyPasswordModel.h"
#import "CountHeight.h"

@interface LCLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong,nonatomic)LoginModel    *loginModel;
@end

@implementation LCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"登录"];
    _loginModel = [[LoginModel alloc]init];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    UIButton  *disBtn = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.topView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(31);
            make.left.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(30, 20));
        }];
        [button addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button;
    });
    [disBtn setTitle:@"取消" forState:UIControlStateNormal];
    _loginBtn.layer.cornerRadius = 3;
    _loginBtn.layer.masksToBounds = YES;
    _password.secureTextEntry = YES;
    [_userName addTarget:self action:@selector(userName:) forControlEvents:UIControlEventEditingChanged];
    [_password addTarget:self action:@selector(password:) forControlEvents:UIControlEventEditingChanged];
}
- (void)disBtnClick:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)userName:(UITextField *)sender
{
    if (sender.text.length > 0)
    {
        _loginModel.userName = sender.text;
    }
}
- (void)password:(UITextField *)sender
{
    if (sender.text.length > 0)
    {
        _loginModel.password = sender.text;
    }
}
- (IBAction)loginBtnClick:(UIButton *)sender
{
    if (_loginModel.userName.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"用户名不能为空!"];
        return;
    }
    if (_loginModel.password.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"密码不能为空!"];
        return;
    }
    NSMutableDictionary *loginDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [loginDic setObject:_loginModel.userName forKey:@"app_user"];
    [loginDic setObject:[CountHeight sha1:[NSString stringWithFormat:@"%@%@",_loginModel.userName,_loginModel.password]] forKey:@"app_pass"];
    [MDataProvider login:loginDic sucess:^(NSDictionary *dic) {
        set_sp(@"app_user", dic[@"app_user"]);//用户名
        set_sp(@"app_pass", dic[@"app_pass"]);//密码
        set_sp(@"company_id", dic[@"appuser"][@"company_id"]);//企业id
        set_sp(@"company_name", dic[@"appuser"][@"company_name"]);//企业名称
        set_sp(@"company", dic[@"company"]);//企业信息
        [self dismissViewControllerAnimated:YES completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:nLogin object:nil];
    } faile:^(NSString *str) {
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
@end
